var Page = function() {
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        page: 1,
        listRows: 10,
        total: 0,
        mobile: '',
        isVip: '',
        order:'desc',
        realName:'',
        eachDistrict:'',
        isUse:'',
        areaId:[],
        area:[],
        areaType:'',
        annualFee:''
    },
    init: function() {
        this.initParams();
        this.initEvent();
    },
    initParams: function() {
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function() {
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page: _this.params.page,
            listRows: _this.params.listRows,
            mobile: _this.params.mobile,
            order: _this.params.order,
            realName: _this.params.realName,
            eachDistrict: _this.params.eachDistrict,
            isUse: _this.params.isUse,
            isVip: _this.params.isVip,
            annualFee:_this.params.annualFee
        };
        console.log(paramsObj);
        Api.userinfolist(paramsObj, function(data) {
            console.log(data);
            _this.getDataList(data.list);
            _this.params.total = data.totalPages;
            layui.use('laypage', function() {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'page',
                    index: 1,
                    theme: '#3498db',
                    curr: _this.params.page,
                    count: _this.params.total, //数据总数，从服务端得到
                    hash: 'curr_page',
                    layout: ['prev', 'page', 'next', 'skip'],
                    jump: function(obj, first) {
                        _this.params.page = obj.curr;
                        //obj包含了当前分页的所有参数，比如：curr当前页
                        var Params = {
                            passport: _this.params.passport,
                            page: _this.params.page,
                            listRows: _this.params.listRows,
                            mobile: _this.params.mobile,
                            order: _this.params.order,
                            realName: _this.params.realName,
                            eachDistrict: _this.params.eachDistrict,
                            isUse: _this.params.isUse,
                            isVip: _this.params.isVip,
                            annualFee:_this.params.annualFee
                        };
                        Api.userinfolist(Params, function(data) {
                            _this.getDataList(data.list);

                        })
                    }
                });
            });
        });
    },
    getDataList: function(data) {
        var _this = this;
        if (data && data.length > 0) {
            var html = '',
                i = 0,
                len = data.length;
            for (i; i < len; i++) {
                html += '<tr>' +

                    '<td class="text-c">' + data[i].mobile + '</td>' +
                    '<td class="text-c">' + data[i].realName + '</td>';
                if (data[i].isVip == 1) {
                    html += '<td class="text-c">是</td>';
                } else {
                    html += '<td class="text-c">否</td>';
                };
                if (data[i].annualFee == 1) {
                    html += '<td class="text-c">是</td>';
                } else {
                    html += '<td class="text-c">否</td>';
                };

                html += '<td class="text-c">' + data[i].team + '</td>' ;

                if (data[i].isAuth == 0) {
                    html += '<td class="text-c">未认证</td>';
                } else if (data[i].isAuth == 1){
                    html += '<td class="text-c">已认证</td>';
                }else if (data[i].isAuth == 2){
                    html += '<td class="text-c">认证中</td>';
                }else if (data[i].isAuth == 3){
                    html += '<td class="text-c">驳回</td>';
                };

                html += '<td class="text-c">' +
                    '<p><a userId="' + data[i].userId + '" mobile="' + data[i].mobile + '" class="handle view">查看</a><a userId="' + data[i].userId + '" mobile="' + data[i].mobile + '" class="handle edit">编辑</a>';
                if (data[i].isUse == 0) {
                    html+='<a userId="' + data[i].userId + '" mobile="' + data[i].mobile + '" isUse="1" class="c-666 isUse" style="padding:2px 5px;">恢复</a>';
                }else{
                    html+='<a userId="' + data[i].userId + '" mobile="' + data[i].mobile + '" isUse="0" class="handle isUse">冻结</a>';
                };
                    html+='</p>' +
                    '</td>' +
                    '</tr>';
            }
            $('#List').html(html);
            $('tfoot').css('display', 'table-footer-group');
            _this.getDataEvent();
        } else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="7" >暂无数据</td>' +
                '</tr>';
            $('#List').html(html);
            $('tfoot').css('display', 'none');
        }
    },
    getDataEvent: function() {
        var _this = this;
        honeySwitch.init();
        $('.view').on('click', function() {
            Pub.setSession('userId', $(this).attr('userId'));
            window.location.href = Pub.getHtmlUrl('./user-list-detail.html');
        });
        $('.edit').on('click', function() {
            Pub.setSession('userId', $(this).attr('userId'));
            window.location.href = Pub.getHtmlUrl('./user-list-edit.html');
        });
        $('.isUse').on('click', function() {
            var userId=$(this).attr('userId');
            var isUse=$(this).attr('isUse');
            var Params = {
                passport: _this.params.passport,
                userId:userId
            };
            console.log(Params);
            if (isUse==1) {
                layui.use('layer', function(){
                    layer.confirm('是否恢复？', {
                        btn: ['确定','取消'] //按钮
                    }, function(index){
                        _this.isfreeze(userId,isUse);
                        layer.close(index);
                    }, function(){

                    });
                });
            }else{
                Api.arrearssuggests(Params, function(data) {
                    console.log(data);
                    layui.use('layer', function(){
                        layer.confirm(data.error, {
                            btn: ['确定','取消'] //按钮
                        }, function(index){
                            _this.isfreeze(userId,isUse);
                            layer.close(index);
                        }, function(){

                        });
                    });
                })
            };

        });
    },
    isfreeze:function(userId,isUse){
        var _this = this;
        var Params = {
            passport: _this.params.passport,
            userId:userId,
            isUse:parseInt(isUse)
        };
        console.log(Params);
        Api.isfreeze(Params, function(data) {
            _this.getData();
        })
    },
    useroperationmodify: function(userId, mobile, datas) {
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            userId: userId,
            mobile: mobile,
            isEnable: datas
        };
        console.log(paramsObj);
        Api.useroperationmodify(paramsObj, function(data) {
            console.log(data);
            // _this.getData();
        });
    },
    otherEvent: function() {
        var _this = this;

        $('#changeSize').on('change', function(event) {
            event.preventDefault();
            _this.params.listRows = $(this).val();
            _this.params.page=1;
            $('#page').html('');
            _this.getData();
        });
        $('#changeDesc').on('change', function(event) {
            event.preventDefault();
            if ($(this).val() !== '排序方式') {
                _this.params.order = $(this).val();
                $('#page').html('');
                _this.params.page=1;
                _this.getData();
            };
        });
        $('#vipSelect').on('change', function(event) {
            event.preventDefault();
            _this.params.isVip = $(this).val();
        });
        $('#vip2Select').on('change', function(event) {
            event.preventDefault();
            _this.params.annualFee = $(this).val();
        });
        $('#accountState').on('change', function(event) {
            event.preventDefault();
            _this.params.isUse = $(this).val();
        });
        $('#localSearch').click(function(event) {
            $('#modal_search').modal('show');
        });
        $('#normalSearch').click(function(event) {
            _this.params.mobile = $('#mobile').val();
            _this.params.realName = $('#realName').val();
            _this.params.page = 1;
            _this.getData();
        });
        $('.slectShow').click(function(event) {
            $('.select-wrap').toggle(400);
        });

        $('#localBtn').click(function(event) {
            if ($("#cnee_city").val()=='z') {
                layui.use('layer', function(){
                    layer.msg('请选择有效地区', {icon: 5});
                });
            }else{
                if ($("#cnee_city").find("option:selected").val()!='') {
                    var pro=$("#cnee_province").find("option:selected").text();
                    var city=$("#cnee_city").find("option:selected").text();
                    _this.params.eachDistrict=pro+city;
                    console.log(_this.params.eachDistrict);
                }else{
                    _this.params.eachDistrict='';
                    console.log(_this.params.eachDistrict);
                };
                $('#modal_search').modal('hide');
                _this.params.page = 1;
                _this.getData();
            };

            // console.log(_this.params.eachDistrict);
        });

    },
    getProvinceData:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport
        };
        Api.areaList(paramsObj, function(data){
            //console.log(data);

            _this.getProvinceList(data.list);
        });
    },
    getProvinceList:function(data){
        var _this = this;
        if (data && data.length>0) {
            var html = '<option value="">全部</option>', i = 0, len = data.length;
            for (i; i < len; i++) {
                html+='<option value="'+data[i].areaId+'">'+data[i].name+'</option>';
            }
            $('#cnee_province').html(html);
            _this.getProvinceEvent();
        };
    },
    getProvinceEvent:function(){
        var _this = this;
        $('#cnee_province').on('change', function(event) {
            event.preventDefault();
            if ($("#cnee_province option:selected").val()!='') {
                _this.getCityData($("#cnee_province option:selected").val());
            }else{
                $('#cnee_city').html('<option value="z">全部</option>');
            };

        });
    },
    getCityData:function(id){
        var _this = this;
        var json={}
        var arr=[];
        if (_this.params.areaType==1) {
            if (id==11 && _this.params.areaId.indexOf(11) !== -1) {
                json.name='北京市';
                json.areaId=11;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==12 && _this.params.areaId.indexOf(12) !== -1) {
                json.name='天津市';
                json.areaId=12;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==31 && _this.params.areaId.indexOf(31) !== -1) {
                json.name='上海市';
                json.areaId=31;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==50 && _this.params.areaId.indexOf(50) !== -1) {
                json.name='重庆市';
                json.areaId=50;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==71 && _this.params.areaId.indexOf(71) !== -1) {
                json.name='台湾省';
                json.areaId=71;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==81 && _this.params.areaId.indexOf(81) !== -1) {
                json.name='香港特别行政区';
                json.areaId=81;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==82 && _this.params.areaId.indexOf(82) !== -1) {
                json.name='澳门特别行政区';
                json.areaId=82;
                arr.push(json)
                _this.getCityList(arr);
            }else{
                var paramsObj = {
                    passport: _this.params.passport,
                    parentId:id
                };
                console.log(paramsObj);
                Api.areaList(paramsObj, function(data){
                    console.log(data);
                    var arr=[];
                    var len = data.list.length,i = 0;
                    for (i; i < len; i++) {
                        _this.params.areaId.forEach(function(item, index){
                            if (data.list[i].areaId==item) {
                                arr.push(data.list[i])
                            };
                        })
                    }
                    console.log(arr);
                    _this.getCityList(arr);
                });
            };
        }else if(_this.params.areaType==0){
            if (id==11) {
                json.name='北京市';
                json.areaId=11;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==12) {
                json.name='天津市';
                json.areaId=12;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==31) {
                json.name='上海市';
                json.areaId=31;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==50) {
                json.name='重庆市';
                json.areaId=50;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==71) {
                json.name='台湾省';
                json.areaId=71;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==81) {
                json.name='香港特别行政区';
                json.areaId=81;
                arr.push(json)
                _this.getCityList(arr);
            }else if(id==82) {
                json.name='澳门特别行政区';
                json.areaId=82;
                arr.push(json)
                _this.getCityList(arr);
            }else{
                var paramsObj = {
                    passport: _this.params.passport,
                    parentId:id
                };
                console.log(paramsObj);
                Api.areaList(paramsObj, function(data){
                    console.log(data);
                    _this.getCityList(data.list);

                });
            };
        }else if(_this.params.areaType==2){
            if (id==11) {
                if (_this.params.areaId.indexOf(11) == -1) {
                    json.name='北京市';
                    json.areaId=11;
                    arr.push(json);
                    _this.getCityList(arr);
                }else{
                    arr=[];
                    _this.getCityList(arr);
                };
            }else if(id==12) {
                if (_this.params.areaId.indexOf(12) == -1) {
                    json.name='天津市';
                    json.areaId=12;
                    arr.push(json);
                    _this.getCityList(arr);
                }else{
                    arr=[];
                    _this.getCityList(arr);
                };
            }else if(id==31) {
                if (_this.params.areaId.indexOf(31) == -1) {
                    json.name='上海市';
                    json.areaId=31;
                    arr.push(json);
                    _this.getCityList(arr);
                }else{
                    arr=[];
                    _this.getCityList(arr);
                };
            }else if(id==50) {
                if (_this.params.areaId.indexOf(50) == -1) {
                    json.name='重庆市';
                    json.areaId=50;
                    arr.push(json);
                    _this.getCityList(arr);
                }else{
                    arr=[];
                    _this.getCityList(arr);
                };
            }else if(id==71) {
                if (_this.params.areaId.indexOf(71) == -1) {
                    json.name='台湾省';
                    json.areaId=71;
                    arr.push(json);
                    _this.getCityList(arr);
                }else{
                    arr=[];
                    _this.getCityList(arr);
                };
            }else if(id==81) {
                if (_this.params.areaId.indexOf(81) == -1) {
                    json.name='香港特别行政区';
                    json.areaId=81;
                    arr.push(json);
                    _this.getCityList(arr);
                }else{
                    arr=[];
                    _this.getCityList(arr);
                };
            }else if(id==82) {
                if (_this.params.areaId.indexOf(82) == -1) {
                    json.name='澳门特别行政区';
                    json.areaId=82;
                    arr.push(json);
                    _this.getCityList(arr);
                }else{
                    arr=[];
                    _this.getCityList(arr);
                };
            }else{
                var paramsObj = {
                    passport: _this.params.passport,
                    parentId:id
                };
                Api.areaList(paramsObj, function(data){
                    //console.log(data);
                    var arr=[];
                    var len = data.list.length,i = 0;
                    for (var i = 0; i < len; i++) {
                        arr.push(data.list[i])
                    };
                    for (var j = 0; j < arr.length; j++) {
                        _this.params.areaId.forEach(function(item, index){
                            if (arr[j].areaId==item) {
                                arr.splice(j,1);
                            };
                        })
                    };

                    console.log(arr);
                    _this.getCityList(arr);
                });
            };
        };;

    },
    getCityList:function(data){
        var _this = this;
        if (data && data.length>0) {
            var html = '', i = 0, len = data.length;
            for (i; i < len; i++) {
                html+='<option value="'+data[i].areaId+'">'+data[i].name+'</option>';
            }
            $('#cnee_city').html(html);
            _this.getCityEvent();
        }else{
            $('#cnee_city').html('<option value="z">未拥有此区域权限</option>');
        };
    },
    getCityEvent:function(){
        var _this = this;
        $('#cnee_city').on('change', function(event) {
            event.preventDefault();

        });
    },
    admineachdistrict:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport
        };
        Api.admineachdistrict(paramsObj, function(data){
            console.log(data);
            _this.params.areaType=data.type;
            for (var i = 0; i < data.addressId.length; i++) {
                _this.params.areaId.push(data.addressId[i]);
                _this.params.area.push(data.area[i]);
            };

        });
    },
    initEvent: function() {
        var _this = this;
         _this.getData();
         _this.otherEvent();
         setTimeout(function(){
            _this.getProvinceData();
         },500)
         _this.admineachdistrict();
    }
};
var page = new Page();